package com.en.util;

import com.en.entity.User;
import com.en.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtils {
    public static Long getCurrentUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || !authentication.isAuthenticated()) {
            throw new RuntimeException("用户未登录");
        }
        
        // 从认证信息中获取用户名
        String username = authentication.getName();
        
        // 从 UserService 中获取用户信息
        UserService userService = SpringContextHolder.getBean(UserService.class);
        User user = userService.getCurrentUser();
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        
        return user.getId();
    }
} 